#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from django import template

register = template.Library()

@register.inclusion_tag('teacher/pagnitor.html', takes_context=True)
def pagination_html(context):

    """
    分页翻页功能
    :param context:
    :return:
    """

    total_pages = int(context.get('total_pages')) # 总页数
    page = int(context.get('current_page')) # 显示第几页的数据
    # page_list = context.get('p').page_range
    per_page = int(context.get('num_perpage')) # 每页显示多少数量

    page_list = list()
    num = 1
    '''
    1.左边 + 当前页显示的页码列表
    '''
    if page - num <= 0: # 1.1左边不够显示时，左边的页码范围：1到当前页
        for i in range(1, page+1):
            page_list.append(i)
    else: # 1.2左边页够显示时，页码范围：page-num 到当前页
        for i in range(page-num, page+1):
            page_list.append(i)

    # 2.右边不够显示时，页码范围：当前页+1到total_pages
    if page + num >= total_pages:
        for i in range(page+1, total_pages+1):
            page_list.append(i)
    else: # 右边页够显示时，页码范围：当前页+1到当前页+num
        for i in range(page+1, page+num+1):
            page_list.append(i)

    return {'total_pages': total_pages, 'page': page, 'page_list': page_list, 'per_page': per_page}

@register.simple_tag
def add_class(field, class_str):

    field.as_widget(attrs={'class': class_str})